package com.dycong.common.database.mongoDB;

import lombok.extern.java.Log;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

/**
 *todo  Javers spring
 *todo RdbBaseService中Javers javers-->javers.commit(JaversUtil.user,object);处使用了javers:
 *todo JaversBuilder.javers()
 *todo              .registerJaversRepository(inMemoryRepository)
 *todo              .build();
 *todo RdbBaseService.get()调用了javers.commit方法，江都渠道的Object放入InMemoryRepository.snapshots
 *todo 放入的时候如果已存在就不再放入，不存在才添加，删除时会移除，update后也会移除，在下次修改前会再次加载。
 *todo 不会占用过多内存。不至于溢出!!!
 */
@Component
@Log
public class JaversFactory implements FactoryBean<Javers> {


    public static InMemoryRepository inMemoryRepository=new InMemoryRepository();

    @Override
    public Javers getObject() throws Exception {
        Javers javers = JaversBuilder.javers()
                .registerJaversRepository(inMemoryRepository)
                .build();
        return javers;
    }


    @Override
    public Class<?> getObjectType() {
        return Javers.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
